package cn.yunhe.service;

import cn.yunhe.dao.BookDao;
import cn.yunhe.pojo.Book;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class BookService {
    SqlSession session=null;
    BookDao bookDao=null;
    
    public void initial(){
        session=SqlSessionFactoryUtil.getSqlSession();
        bookDao=session.getMapper(BookDao.class);
    }
    
    public void destory(){
        if (session!=null){
            session.commit();
            session.close();
        }
    }

    public List<Book> queryByBname(String bname){
        initial();
        List<Book> bookList=bookDao.queryByBname(bname);
        destory();
        return bookList;

    }


    public List<Book> queryByBauthor(String bauthor){
        initial();
        List<Book> bookList=bookDao.queryByBauthor(bauthor);
        destory();
        return bookList;
    }


    public List<Book> queryByBpress(String bpress){
        initial();
        List<Book> bookList=bookDao.queryByBpress(bpress);
        destory();
        return bookList;
    }

    public String[] searchBname(String keywords){
        initial();
        String[] bname2Array=bookDao.searchBname(keywords);
        destory();
        return bname2Array;
    }
    
    public boolean insertBook1(String bisbn, String bname, String bauthor, String bpress, double bprice, int bcount, String bpic){
        initial();
        Book book=new Book(bisbn,bname,bauthor,bpress,bprice,bcount,bpic);
        boolean flag= bookDao.insertBook(book);
        destory();
        return flag;
    }

    public boolean insertBook(Book book){
        initial();
        boolean flag=bookDao.insertBook(book);
        destory();
        return flag;
    }

    public Book searchBook(int bid){
        initial();
        Book book=bookDao.searchBook(bid);
        destory();
        return book;
    }

    public boolean updateBook(Book book){
        initial();
        boolean flag=bookDao.updateBook(book);
        destory();
        return flag;
    }

    public boolean deleteBook(int bid){
        initial();
        boolean flag=bookDao.deleteBook(bid);
        destory();
        return flag;
    }
}
